package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.entity.RecordEquipmentOperate;
import com.yqg.protection.mapper.RecordEquipmentOperateMapper;
import com.yqg.protection.service.IRecordEquipmentOperateService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * RecordEquipmentOperateController 前端控制器
 *
 * @author 小宇
 * @since 2024-09-13
 */
@RestController
@RequestMapping("/record-equipment-operate")
public class RecordEquipmentOperateController {

    @Resource
    private IRecordEquipmentOperateService recordEquipmentOperateService;

    @Autowired
    private RecordEquipmentOperateMapper recordEquipmentOperateMapper;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者修改设施操作记录")
    public Result save(@RequestBody RecordEquipmentOperate recordEquipmentOperate) {
        recordEquipmentOperate.setIsDelete(0);
        return Result.success(recordEquipmentOperateService.saveOrUpdate(recordEquipmentOperate));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    @SysLog("删除单个设施操作记录")
    public Result delete(@PathVariable Integer id) {
        RecordEquipmentOperate recordEquipmentOperate = recordEquipmentOperateService.getById(id);
        recordEquipmentOperate.setIsDelete(1);
        return Result.success(recordEquipmentOperateService.saveOrUpdate(recordEquipmentOperate));
    }

    @PostMapping("/deleteByIdList")
    @SysLog("删除多个设施操作记录")
    public Result deleteByIdList(@RequestBody List<Long> id) {
        List<RecordEquipmentOperate> recordEquipmentOperates = recordEquipmentOperateService.listByIds(id);
        for (RecordEquipmentOperate recordEquipmentOperate:recordEquipmentOperates) {
            recordEquipmentOperate.setIsDelete(1);
        }
        return Result.success(recordEquipmentOperateService.saveOrUpdateBatch(recordEquipmentOperates));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<RecordEquipmentOperate> queryWrapper = new QueryWrapper<>();
        List<RecordEquipmentOperate> list = recordEquipmentOperateService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(recordEquipmentOperateService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String keyWord,
                           String startTime,
                           String endTime,
                           Integer level,
                           Long organizationId,
                           String isAllClient,
                           @RequestParam List<Long> clientIds) {
        pageNum = pageNum * pageSize - pageSize;
        List<RecordEquipmentOperate> recordEquipmentOperates = recordEquipmentOperateMapper.findPage(pageNum, pageSize, keyWord, startTime, endTime, level, organizationId, isAllClient, clientIds);
        Long count = recordEquipmentOperateMapper.findPageCount(keyWord, startTime, endTime, level, organizationId, isAllClient, clientIds);
        HashMap<String, Object> map = new HashMap<>();
        map.put("record", recordEquipmentOperates);
        map.put("total", count);
        return Result.success(map);
    }

    @ApiOperation("导出数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       String keyWord,
                       String startTime,
                       String endTime,
                       Integer level,
                       Long organizationId,
                       String isAllClient,
                       @RequestParam List<Long> clientIds) {
        recordEquipmentOperateService.export(response, keyWord, startTime, endTime, level, organizationId, isAllClient, clientIds);
    }

    @ApiOperation("导入数据")
    @PostMapping("/importData")
    @SysLog("导入设施操作记录")
    public void importData(MultipartFile file) {
        recordEquipmentOperateService.importData(file);
    }
}

